package tongwentongshu.com.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import java.util.ArrayList;

import butterknife.BindView;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.fragment.BaseFragmentActivity;
import tongwentongshu.com.app.fragment.BootPageFragment;

public class BootPageActivity extends BaseFragmentActivity {
    /**
     * 页面展示ViewPager
     */
    @BindView(R.id.pager)
    ViewPager pager;
    /**
     * 主页加载的Fragment
     */
    private ArrayList<Fragment> fragments;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_boot_page;
    }

    @Override
    protected int getTitleId() {
        return R.string.app_name;
    }

    @Override
    protected void initView() {
        initViewPager();
    }

    @Override
    protected void initData() {

    }

    private void initViewPager() {
        fragments = new ArrayList<Fragment>();
        for (int i = 0; i < 4; i++) {
            Bundle data = new Bundle();
            data.putString(Cache.INDEX, String.valueOf(i + 1));
            BootPageFragment fragment = new BootPageFragment(this);
            fragment.setArguments(data);
            fragments.add(fragment);
        }
        MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
                getSupportFragmentManager(), fragments);
        pager.setAdapter(fragmentPagerAdapter);
        fragmentPagerAdapter.setFragments(fragments);
//        pager.setOnPageChangeListener(new MyOnPageChangeListener());
        pager.setCurrentItem(0);
    }

    private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> fragments;
        private FragmentManager fm;

        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            this.fm = fm;
        }

        public MyFragmentPagerAdapter(FragmentManager fm,
                                      ArrayList<Fragment> fragments) {
            super(fm);
            this.fm = fm;
            this.fragments = fragments;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        public void setFragments(ArrayList<Fragment> fragments) {
            if (this.fragments != null) {
                FragmentTransaction ft = fm.beginTransaction();
                for (Fragment f : this.fragments) {
                    ft.remove(f);
                }
                ft.commit();
                ft = null;
                fm.executePendingTransactions();
            }
            this.fragments = fragments;
            notifyDataSetChanged();
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            Object obj = super.instantiateItem(container, position);
            return obj;
        }

    }
}
